<?php
	
	require 'ccb.class.php';
	/**
	 * 插入数据
	 * 
	 * @param $data unknown_type       	
	 */
	function save($data, $table) {
		global $db;
		$sqlk = $sqlv = '';
		foreach ( $data as $k => $v ) {
			
			$sqlk .= ',' . $k;
			$sqlv .= ",'$v'";
		
		}
		$sqlk = substr ( $sqlk, 1 );
		$sqlv = substr ( $sqlv, 1 );
		// echo $sqlk.$sqlv;
		// return ;
		$sql = "INSERT INTO {$table} ($sqlk) VALUES ($sqlv)";
		// echo $sql;
		$db->query ( $sql );
		$id = $db->insert_id ();
		return $id;
	}
	/**
	 * 更新数据
	 * 
	 * @param $data unknown_type       	
	 */
	function update($data, $table, $key, $val) {
		global $db;
		$sqlk = $sqlv = '';
		foreach ( $data as $k => $v ) {
			
			$sqlk .= ',' . $k . "='" . $v . "'";
		
		}
		$sqlk = substr ( $sqlk, 1 );
		$sql = "update {$table} set $sqlk where $key='$val'" ;
		
		// echo $sqlk.$sqlv;
		// return ;
		//echo $sql;
		$rt = $db->query ($sql );
		
		return $rt;
	}
	/**
	 * 删除数据
	 * 
	 * @param $data unknown_type       	
	 */
	function delete($table, $key, $val) {
		global $db;
		$sql = "delete from $table where $key='$val'";
		
		// echo $sqlk.$sqlv;
		// return ;
		$rt = $db->query ( $sql );
		
		return $rt;
	}

	/**
	 * 会员信息传输
	 */
	function ccb_3FC001($userid) {
		/*
		 * 查询会员信息
		 */
		global $db;
		$ccb = new ccb ();
		$user = $db->get_one ( "SELECT * FROM b_member WHERE userid=$userid" );
		if ($user)
			$username = $user ['username'];
			// var_dump($user);
		
		$data = array ();
		
		$data ['head'] ['chanl_trad_no'] = '3FC001';
		$data ['head'] ['type'] = '0200';
		$data ['field'] ['MCH_NO'] = $ccb->merchant_no; // 商户编号
		$data ['field'] ['MBR_CERT_TYPE'] = $user ['mbrcerttype']; // 会员证件类型
		$data ['field'] ['MBR_CERT_NO'] = $user ['mbrcertno']; // 会员证件号码
		$data ['field'] ['SPOT_SIT_NO'] = $user ['userid']; // 实时记账模式席位号
		$data ['field'] ['FUTURE_SIT_NO'] = ''; // 日终清算模式席位号
		$data ['field'] ['MBR_NAME'] = $user ['company']; // 会员名称
		$data ['field'] ['MBR_SPE_ACCT_NO'] = $user ['mbrspeacctno']; // 会员指定账号
		$data ['field'] ['MBR_SPE_ACCT_BKID'] = '0'; // 会员账号行别
		$data ['field'] ['MBR_CONTACT'] = $user ['truename']; // 会员联系人
		$data ['field'] ['MBR_PHONE_NUM'] = $user ['mobile']; // 会员联系方式
		$data ['field'] ['MBR_ADDR'] = $user ['memaddr']; // 会员地址
		$data ['field'] ['MBR_ANNUAL_FEE_AMT'] = ''; // 会员年费金额
		$data ['field'] ['MBR_INOUT_AMT_SVC_AMT'] = ''; // 会员出入金手续费金额
		$data ['field'] ['MBR_INOUT_AMT_SVC_DRAWEE'] = '1'; // 会员出入金手续费付费方
		$data ['field'] ['MBR_INOUT_AMT_SVC_RCV_STY'] = '0'; // 会员出入金手续费收取方式
		$data ['field'] ['SIGNED_DATE'] = $user ['sigiinddate']; // 签约日期
		$data ['field'] ['DIS_SGN_DATE'] = ''; // 解约日期
		$data ['field'] ['MBR_STS'] = $user ['MBR_STS']; // 会员状态
		$data ['field'] ['RMRK'] = ''; // 备注
		
		$ccb_rt = $ccb->doComm ( $data );
		
		return $ccb_rt;
	}
	
	/**
	 * 会员入金
	 */
	function ccb_3FC002($post, $userid) {
		
		$table = 'b_ccb_money_in';
		$rt_fields = array ();
		$data = array ();
		
		$ccb = new ccb ();
		
		$data ['head'] ['type'] = '0200';
		$data ['head'] ['chanl_trad_no'] = '3FC002';
		
		$data ['field'] ['MCH_NO'] = $ccb->merchant_no; // 商户编号
		$data ['field'] ['TX_AMT'] = $post ['TX_AMT'];
		$data ['field'] ['RMRK'] = $post ['RMRK'];
		$data ['field'] ['CURR_COD'] = '01';
		$data ['field'] ['IN_AMT_SIT_NO'] = $userid;
		
		$ccb_rt = $ccb->doComm ( $data );
		// var_dump($ccb_rt) ;
		
		if ($ccb_rt ['HEAD'] ['resp_code'] == '000000000000') {
			$rt_fields = $ccb_rt ['BODY'] ['FIELD'];
			$rt_fields ['userid'] = $userid;
			
			save ( $rt_fields, $table );
		}
		
		return $ccb_rt;
	
	}
	
	/**
	 * 会员出金
	 */
	function ccb_3FC022($post) {
		
		$table = 'b_ccb_money_out';
		$ccb = new ccb ();
		
		$rts = array ();
		$data = array ();
		
		// 保存出金申请
		$data ['field'] ['MCH_NO'] = $post ['MCH_NO'];
		// $FLOW_NO='';申请记录流水id
		$data ['field'] ['DRAWEE_ACCT_NO'] = $post ['DRAWEE_ACCT_NO'] ;
		$data ['field'] ['PAYEE_ACCT_NO'] = $post ['PAYEE_ACCT_NO'];
		$data ['field'] ['CURR_COD'] = '01'; // 币别
		$data ['field'] ['TX_AMT'] = $post ['TX_AMT']; // 金额
		$data ['field'] ['OUT_AMT_SIT_NO'] = $post ['OUT_AMT_SIT_NO']; // 出金席位号
		$data ['field'] ['AUDIT_STS'] = $post ['AUDIT_STS']; // 审批结果: 1:通过2不通过
		$data ['field'] ['RMRK'] = $post ['RMRK'];
			
		$data ['field'] ['FLOW_NO'] = $post ['id'];
		// echo $data['$FLOW_NO'];
		
		$data ['head'] ['type'] = '0200';
		$data ['head'] ['chanl_trad_no'] = '3FC022';
		//var_dump($data) ;
		$ccb_rt = $ccb->doComm ( $data );
		//var_dump($ccb_rt) ;
		if ($ccb_rt ['HEAD'] ['resp_code'] == '000000000000') {
			
			$rt_OUT_TX_LOG_NO = $ccb_rt ['BODY'] ['FIELD'] ['OUT_TX_LOG_NO'];
			$u_data = array ();
			
			if ($rt_OUT_TX_LOG_NO) {				
				$u_data ['FLOW_NO'] = $rt_OUT_TX_LOG_NO;
				}
				
			$u_data ['AUDIT_STS'] = $post ['AUDIT_STS'];
			update ( $u_data, $table, 'id', $post ['id'] );
		} else {
			// delete ( $table, 'id', $insID );
			$ccb_rt ['HEAD'] ['resp_code'] == '';
		}
			
			return $ccb_rt;
	}
	
	/**
	 * 会员出金申请
	 * 银行->商户
	 */
	function ccb_3FC003($c_data) {
		$table='b_ccb_money_out';
		
		$data ['head'] ['type'] = '0210';
		$data ['head'] ['chanl_trad_no'] = '3FC003';
		$data ['head'] ['resp_code'] = '000000000000';
		$data ['head'] ['resp_msg'] = 'SUCCESS';
		
		$c_data["BODY"]['FIELD']['AUDIT_STS']=3;
		save ( $c_data["BODY"]['FIELD'], $table );
		

		//log_write ( '会员出金申请'.serialize($c_data), 'ccb' );
		//log_write ( '会员出金申请返回报文'.serialize($data), '3FC003' );
		return $data;
	}
	
	
	?>